package com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.TongJiInfo;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.ICaseSelectionServiceAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author py
 * @Date
 */
@Controller
@RequestMapping("/caseSelectionAdmin")
public class CaseSelectionAdminController extends BaseController {
    @Autowired
    ICaseSelectionServiceAdmin service;

    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/remoteSynergy/";

    /**
     * 跳转到市场预约  未到
     */
    @RequestMapping("")
    public String to_caseSelectionAdmin(Model model) {
        String tableHead = ShiroKit.getSessionAttr("abbreviation");
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "/case_selection_admin.html";
    }

    @RequestMapping("/explanation")
    public String explanation(Model model) {
        String tableHead = ShiroKit.getSessionAttr("abbreviation");
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "/case_selection_explanation.html";
    }

    /**
     * 病例上传管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object getInfoList(SearchCaseSelectionAdminInfo info, HttpServletRequest request) {
        Page<CaseSelectionAdminInfo> page = new PageFactory<CaseSelectionAdminInfo>().defaultPage();
        try {
            if (info != null) {
                if (info.getIsSelection() == 2) {
                    info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
                    info.setLimitIndex(page.getSize());
                    page.setCurrent(0);
                    List<CaseSelectionAdminInfo> list = service.selectPageListByQueryGetGoBack(page, info);
                    if (list.size() == 0) {
                        info.setStartIndex(0);
                        list = service.selectPageListByQueryGetGoBack(page, info);
                    }
                    page.setTotal(service.getByQueryGetGoBackPagesCount(page, info));
                    page.setRecords(list);
                } else {
                    info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
                    info.setLimitIndex(page.getSize());
                    page.setCurrent(0);
                    List<CaseSelectionAdminInfo> list = service.selectPageListByQueryGetMap(page, info);
                    if (list.size() == 0) {
                        info.setStartIndex(0);
                        list = service.selectPageListByQueryGetMap(page, info);
                    }
                    page.setTotal(service.getQueryPagesCount(page, info));
                    page.setRecords(list);
                }
            } else {
                info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
                info.setLimitIndex(page.getSize());
                page.setCurrent(0);
                List<CaseSelectionAdminInfo> list = service.selectPageListByQueryGetMap(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = service.selectPageListByQueryGetMap(page, info);
                }
                page.setTotal(service.getQueryPagesCount(page, info));
                page.setRecords(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 病例评分统计数量
     */
    @RequestMapping("/caseSelectiontotal")
    @ResponseBody
    public List<TongJiInfo> caseSelectiontotal(SearchCaseSelectionAdminInfo searchInfo, HttpServletRequest request) {
        return service.caseSelectiontotal(searchInfo);
    }

    @RequestMapping(value = "/goback", method = RequestMethod.POST)
    @ResponseBody
    public int goback(String id, String gobackRemarks, Model model) {
        String sql = "update case_selection_information set save_status=0 where id='" + id + "'";
        String sqlin = "INSERT INTO `go_back_case_info` (`back_time`, `back_admin`, `backed_id`,backed_remark) VALUES\n" +
                "('" + DateTimeKit.now() + "', '" + ShiroKit.getUser().getAccount() + "', '" + id + "', '" + gobackRemarks + "');";
        sqlMapper.update(sql);
        return sqlMapper.insert(sqlin);
    }
}